package bzapps.weynak;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.Window;
import android.view.WindowManager;
import bzapps.weynak.fb.FbClient;

public class Splash extends Activity {

	private SharedPreferences pref;
	public static Handler handler;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.splash);
		initHandler();
		
		pref = PreferenceManager.getDefaultSharedPreferences(this);
		if(pref.getBoolean("eula", false)){
			authent();
		}
		else{
			new Eula(this).show();
			pref.edit().putBoolean("isPushRegistered", false).commit();
		}
		
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		}).start();
	}

		
			
		
			
		
		
private void initHandler(){
	handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {		
			if (msg.what==1){
				Intent i = new Intent(getBaseContext(), Maps.class);
				startActivity(i);
				Splash.this.finish();
			}
			else if(msg.what==2){
				Intent i = new Intent(getBaseContext(), FbClient.class);
				startActivity(i);
				Splash.this.finish();
			}
			else if(msg.what==3){
				authent();
			}
		}
	};
}
	
	public void authent() {
		
		int myuid = Integer.parseInt(pref.getString("uid", "0"));
		if (myuid == 0) {
				handler.sendEmptyMessageDelayed(2, 1000);
		}
		else {
			handler.sendEmptyMessageDelayed(1, 3000);
		}

	}
	
	@Override
	public void onAttachedToWindow() {
	    super.onAttachedToWindow();
	    Window window = getWindow();
	    window.setFormat(PixelFormat.RGBA_8888);
	}
	
	
}